package com.zh.controller;

import com.zh.common.BaseResponse;
import com.zh.common.ErrorCode;
import com.zh.common.ResultUtils;
import com.zh.exception.ThrowUtils;
import com.zh.service.ShortLinkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "LinkController", value = "链接控制器")
@RestController
@RequestMapping("/link")
@Slf4j
public class LinkController {

    private final ShortLinkService shortLinkService;

    public LinkController(ShortLinkService shortLinkService) {
        this.shortLinkService = shortLinkService;
    }

    @GetMapping
    @ApiOperation(value = "resolveShortLink", notes = "解析短链接")
    public BaseResponse<String> resolveShortLink(String url) {
        ThrowUtils.throwIf(null == url, ErrorCode.PARAMS_ERROR);
        String link = shortLinkService.resolveShortLink(url);
        return ResultUtils.success(link);
    }

    @GetMapping("/distro")
    @ApiOperation(value = "distroShortLink", notes = "销毁链接")
    public BaseResponse<String> distroShortLink(String url) {
        ThrowUtils.throwIf(null == url, ErrorCode.PARAMS_ERROR);
        shortLinkService.distroShortLink(url);
        return ResultUtils.success("销毁成功");
    }
}
